package com.test.demo.base

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.RequiresApi
import androidx.databinding.ViewDataBinding
import com.gyf.immersionbar.ImmersionBar
import com.test.demo.R
import com.test.demo.ext.dismissLoadingExt
import com.test.demo.ext.showLoadingExt
import me.hgj.jetpackmvvm.base.activity.BaseVmDbActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel


/**
 * 时间　: 2019/12/21
 * 作者　: hegaojian
 * 描述　: 你项目中的Activity基类，在这里实现显示弹窗，吐司，还有加入自己的需求操作 ，如果不想用Databind，请继承
 * BaseVmActivity例如
 * abstract class BaseActivity<VM : BaseViewModel> : BaseVmActivity<VM>() {
 */
abstract class BaseActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmDbActivity<VM, DB>() {

    abstract override fun layoutId(): Int

    abstract override fun initView(savedInstanceState: Bundle?)

    override fun initStatusBar() {
        //设置共同沉浸式样式
        ImmersionBar.with(this)
            .statusBarColor(R.color.color_black_121212)
            .statusBarDarkFont(false)
            .navigationBarColor(R.color.white).init()

    }


    open fun shouldShowDialog(): Boolean {
        return true
    }

    val windowManager1 by lazy {
        getSystemService(android.content.Context.WINDOW_SERVICE) as WindowManager
    }

    @RequiresApi(Build.VERSION_CODES.M)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setFitsSystemWindows(true)
        if (!Settings.canDrawOverlays(this)) {
            val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
            startActivityForResult(intent, 1)
        }

    }

    /**
     * 设置根布局的 fitsSystemWindows 属性
     * @param fitsSystemWindows 是否启用 fitsSystemWindows
     */
    fun setFitsSystemWindows(fitsSystemWindows: Boolean) {
        val contentView = findViewById<View>(android.R.id.content)
        if (contentView is ViewGroup && contentView.childCount > 0) {
            val rootView = contentView.getChildAt(0)
            rootView.fitsSystemWindows = fitsSystemWindows
        }
    }


    override fun needEventBusOpen() {

    }

    override fun initListener() {
    }

    open fun openEventBus(): Boolean {
        return false
    }

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 1) {

        }
    }

}